﻿/*
 * @Autor: PK
 * @Date: 2021-11-29 13:34:14
 */

using System.Collections.Generic;
using System.Data;
using ShkSimulation.core.exceptions;
using ShkSimulation.core.factory;
using ShkSimulation.core.util;

namespace ShkSimulation.core.template.customer {

	public abstract class DemandManagerByCreateParam: DemandManager {

		protected readonly string demandResourceKey;

		public DemandManagerByCreateParam(string demandResourceKey) {
			this.demandResourceKey = demandResourceKey;
		}

		public override List<CustomerTemplate> InitializeCustomerTemplate(NetworkInputData environment) {
		    if (environment == null) {
			    throw new ErrorParamException("路网环境为空");
		    }
		    this.NetworkInputData = environment;
		    this.DemandData = environment.AllResources.GetValueOrDefault(demandResourceKey);
		    this.buffer = new Dictionary<string, CustomerTemplate>();
		    this.Templates = CreateCustomerTemplate();
		    return Templates;
	    }
	    
	    private List<CustomerTemplate> CreateCustomerTemplate() {
		    DataTable dataTable = DemandData.GetDataTable();
		    List<CustomerTemplate> result = new List<CustomerTemplate>();
		    List<CreateParam> lines = CollectionUtil.DataTableToCreateParams(dataTable);
		    foreach (CreateParam lineData in lines) {
			    CustomerTemplate template = CreateTemplate(lineData);
			    if (template == null) {
				    throw new ErrorParamException($"CustomerTemplate创建为空：{lineData}");
			    }
			    logger.Info($"创建需求模板 {template.UniqueKey}");
			    result.Add(template);
		    }
		    return result;
	    }

	    public abstract CustomerTemplate CreateTemplate(CreateParam lineData);
	    
	}
}
